<?php

/**
 * 	本果用户模块
 *	用户注册时, 要根据 inviteCode 来确定上级 userID.
 *  注册 和 修改用户信息时, 都要同时修改 bg_users_relationships 里面的 niceName.
 */

namespace McAdmin\Controller;
use Common\Controller\HomebaseController;
use BGapi\Model\RestModel as Rest;

class McAdminController extends HomebaseController {

	function _initialize(){}
	
	/*
	*	得到IP地址.
	*/
	function getip()
	{
	    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	    {
	        $cip = $_SERVER["HTTP_CLIENT_IP"];
	    }
	    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
	    {
	        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	    }
	    else if(!empty($_SERVER["REMOTE_ADDR"]))
	    {
	        $cip = $_SERVER["REMOTE_ADDR"];
	    }
	    else
	    {
	        $cip = '';
	    }

	    preg_match("/[\d\.]{7,15}/", $cip, $cips);
	    $cip = isset($cips[0]) ? $cips[0] : 'unknown';
	    unset($cips);
	    return $cip;
	}
	
	/*
	*	超级管理员注册(帮别人注册)
	*/
	public function registerSuper(){

		$mobile = $_POST['mobile'];
		$pass = $_POST['pass'];
		$mobileAdmin = $_POST['mobileAdmin'];
		$passAdmin = $_POST['passAdmin'];

		$code = C('retJson');

		/* 传入参数不能为空 */ 
		if ($mobile == '' || $pass == '' || $mobileAdmin == '' || $passAdmin == ''){
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ( $rejson );
			return;
		}

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		/* 检查手机号码是否已经注册 */
		$user = D('McUser');

		/*1, 检查手机号码是否已经注册 */
		$reg = $user -> check_mobile_registered($mobile);
		if ($reg){
			/* 1, 手机号码注册, 手机号码已经注册 */
			$rejson['retCode'] = '-104';
			$rejson['retMsg'] = $code['-104'];

			$this -> ajaxReturn($rejson);
			return;
		}

		/* 管理员登录 */		
		$login = $user -> login_admin($mobileAdmin,$passAdmin);
		if (! $login){
			/* 账号或密码错误 */
			$rejson['retCode'] = "-105";
			$rejson['retMsg'] = $code['-105'];		
		}
		else{
			/* 验证码正确, 保存注册信息 */
			$ip = $this -> getip();
			$user -> save_register_info($mobile, $pass, $ip);
		}

		$this->ajaxReturn($rejson); 
	}

	/*
	*	提现管理
	*/
	public function tiXianAdmin(){
		$mobileAdmin = $_POST['mobileAdmin'];
		$passAdmin = $_POST['passAdmin'];

		$userID = $_POST['userID'];
		$tiXianJinE = $_POST['tiXianJinE'];
		$tiXianID = $_POST['tiXianID'];

		$code = C('retJson');

		/* 传入参数不能为空 */ 
		if ($userID == '' || $tiXianJinE == '' || $tiXianID == '' || $mobileAdmin == '' || $passAdmin == ''){
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ( $rejson );
			return;
		}

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		/* 管理员登录 */		
		$user = D('McUser');
		$login = $user -> login_admin($mobileAdmin,$passAdmin);
		if (! $login){
			/* 账号或密码错误 */
			$rejson['retCode'] = "-105";
			$rejson['retMsg'] = $code['-105'];	
			$this->ajaxReturn($rejson); 
			return;	
		}

		/* 检查提现信息是否存在 */
		$tiXian = D('BgWalletTiXian');

		$rejson = $tiXian -> apply_cash_opr($userID, $tiXianJinE, $tiXianID);
		if ($rejson['retCode'] == '1'){
			$wallet = D('BgWallet');
			$wallet -> apply_cash_opr($userID, $tiXianJinE, $tiXianID);

			/*   返回提现信息   */
			$data['userID'] = $userID;
			$data['tiXianJinE'] = $tiXianJinE;
			$rejson['data'] = $data;

			$this->ajaxReturn($rejson); 
			return;

		}else{
			$this->ajaxReturn($rejson); 
			return;
		}

	}

	/**
	 * 开市之前发送一条短信
	 * @return [type] [description]
	 */
	public function sendSmsOpening(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];
		$tradeDate = $_POST['tradeDate'];

		/* 数据参数 ticker 不能为空 */
		if ($userID == '' || $token == ''){
			$this -> return_data_error('-2');
			return;
		}else{
			/* 检查 token 是否正确 */ 
			if($this -> compare_token($userID, $token) === false){
				return;
			}
		}

		// 默认为今天的日期.
		if ($tradeDate == '')
			$tradeDate = date('Y-m-d');

		// 读出 userID 信息.
	    $user = D('McUser');	    
	    $info = $user -> read_user_info($userID);
	    $mobile = $info['mobile'];
	    
	    // 检查 mobile 是否正确.
	    $wxdev = C('wxdev');
	    $notify_mobile = $wxdev['notify_mobile'];

	    if($mobile != $notify_mobile){
	    	$this -> return_data_error('-001');
			return;
	    }

	    // 读出IPO条数.
	    $ipo = D('EquIpo');
	    $ipo_list = $ipo -> get_ipo_of_date($tradeDate);
	    $secList = '无';
	    if(count($ipo_list) > 0){
	    	$secList = '';
	    	for($i = 0;$i<count($ipo_list);$i++){
	    		$secList = $secList . $ipo_list[$i]['secShortName'] . ',';
	    	}
	    	$secList = substr($secList, 0, strlen($secList)-1);
	    }

	    // 读出分红
	    $exDiv = D('ExDiv');
	    $exdiv_list = $exDiv -> get_exdiv_of_date($tradeDate);
	    $exList = '无';
	    if(count($exdiv_list) > 0){
	    	$exList = '';
	    	for($i = 0; $i < count($exdiv_list) ; $i++){
	    		$exList = $exList . $exdiv_list[$i]['ticker'] . ',';
	    	}
	    	$exList = substr($exList, 0, strlen($exList)-1);
	    	
	    	// 根据 ticker 列表, 寻找 secShortName .
	    	$exList = $ipo -> get_secShortName_list($exList);
	    }

	    // 准备短信内容.
	    $datas_sms = $this -> get_month_day($tradeDate) . $this -> get_week($tradeDate);
	    $datas_sms = $datas_sms . ': IPO' . count($ipo_list) . '(' . $secList . ')' . '; ExDiv' . count($exdiv_list) . '(' . $exList . ')';

	    // 发送短信
	    $my_sms = A('MySms');
	    $smsStatusCode = $my_sms -> sendSmsOpening($mobile, $datas_sms);

	    $data['mobile'] = $mobile;
	    $data['datas_sms'] = $datas_sms;
	    $data['smsStatusCode'] = $smsStatusCode;
	    $this -> return_data($data);
	}

	/**
	 * 中午发送的短信, 读最新指数价格.
	 * @return [type] [description]
	 */
	public function sendSmsNooning(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];
		$tradeDate = $_POST['tradeDate'];

		/* 数据参数 ticker 不能为空 */
		if ($userID == '' || $token == ''){
			$this -> return_data_error('-2');
			return;
		}else{
			/* 检查 token 是否正确 */ 
			if($this -> compare_token($userID, $token) === false){
				return;
			}
		}

		// 默认为今天的日期.
		if ($tradeDate == '')
			$tradeDate = date('Y-m-d');

		// 读出 userID 信息.
	    $user = D('McUser');	    
	    $info = $user -> read_user_info($userID);
	    $mobile = $info['mobile'];
	    
	    // 检查 mobile 是否正确.
	    $wxdev = C('wxdev');
	    $notify_mobile = $wxdev['notify_mobile'];

	    if($mobile != $notify_mobile){
	    	$this -> return_data_error('-001');
			return;
	    }

	    // 读出指数信息.
	    $mktIndex = D('MktIndex');
	    $indexList = array('000001' ,'399001','000300' );			
	    $mktIndexList2 = $mktIndex->getIndicesListOfMkt($indexList);
	    $strList = '无';
	    if(count($mktIndexList2) > 0){
	    	$strList = '';
	    	for($i = 0; $i<count($mktIndexList2); $i++){
	    		$strList = $strList . $mktIndexList2[$i]['ticker'] . '(';
	    		$strList = $strList . round($mktIndexList2[$i]['closeIndex'], 2) . ',';
	    		$strList = $strList . ($mktIndexList2[$i]['CHGPct'] * 100) . '%),';
	    	}
	    	// $strList = substr($strList, 0, strlen($strList)-1);
	    }

	    // 读本果指数
	    $bgIndex = D('BgIndex');
	    $indexList = array('bg015' ,'bg050' ,'bg100' ,'bg200' );
	    $bgIndexList2 = $bgIndex->getIndicesOfBg($indexList);
	    if(count($bgIndexList2) > 0){
	    	for($i = 0; $i < count($bgIndexList2) ; $i++){
	    		$strList = $strList . $bgIndexList2[$i]['ticker'] . '(';
	    		$strList = $strList . $bgIndexList2[$i]['closeIndex'] . ',';
	    		$strList = $strList . ($bgIndexList2[$i]['CHGPct']*100) . '%),';
	    	}
	    	$strList = substr($strList, 0, strlen($strList)-1);
	    }
	    $strList = str_replace('bg','700' ,$strList);

	    // 准备短信内容.
	    $datas_sms = $this -> get_month_day($tradeDate) . $this -> get_week($tradeDate);
	    $datas_sms = $datas_sms . ': ' . $strList;

	    // 发送短信
	    $my_sms = A('MySms');
	    $smsStatusCode = $my_sms -> sendSmsOpening($mobile, $datas_sms);

	    $data['mobile'] = $mobile;
	    $data['datas_sms'] = $datas_sms;
	    $data['smsStatusCode'] = $smsStatusCode;
	    $this -> return_data($data);
	}

	/**
	 * 计算日期对应的星期
	 * @param  [type] $tradeDate [description]
	 * @return [type]            [description]
	 */
	private function get_week($tradeDate){
		$time = strtotime($tradeDate);
		// $time = time();
		
		return date('D', $time); // 星期中的第几天，文本表示，3 个字母    Mon 到 Sun
	}

	/**
	 * 得到日期的月和日.
	 * @param  [type] $tradeDate [description]
	 * @return [type]            [description]
	 */
	private function get_month_day($tradeDate){
	        //强制转换日期格式
	        $date_str=date('Y-m-d',strtotime($tradeDate));
	   
	        //封装成数组
	        $arr=explode("-", $date_str);
	        
	        //参数赋值
	        //年
	        $year=$arr[0];
	        
	        //月，输出2位整型，不够2位右对齐
	        $month=sprintf('%02d',$arr[1]);
	        
	        //日，输出2位整型，不够2位右对齐
	        $day=sprintf('%02d',$arr[2]);
	        
	        //时分秒默认赋值为0；
	        $hour = $minute = $second = 0;   
	        
	        //转换成时间戳
	        $strap = mktime($hour,$minute,$second,$month,$day,$year);
	        
	        //获取数字型星期几
	        $number_wk=date("w",$strap);
	        
	        //自定义星期数组
	        $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
	        
	        //获取数字对应的星期
	        // return $weekArr[$number_wk];
	        
	        return $month . $day;
	    }
}